#!/bin/bash

target="i386-softmmu"
plugin_path=$(pwd)

decaf_path=""
tcg_taint="no"
trace_version="50"
state_version="20"
show_help="yes"

for opt do
  optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case "$opt" in
  --help|-h) show_help="yes"
  ;;
  --decaf-path=*) decaf_path=`readlink -e "$optarg"`; show_help="no"
  ;;
  --latest) trace_version="60"; state_version="40"
  ;;
  --trace60) trace_version="60"
  ;;
  --trace50) trace_version="50"
  ;;
  --state40) state_version="40"
  ;;
  --state20) state_version="20"
  ;;
# UNSUPPORTED yet
  --enable-taint) tcg_taint="yes"
  ;;
  --disable-taint) tcg_taint="no"
  ;;
  *) echo "ERROR: unknown option $opt"; show_help="yes"
  ;;
  esac
done

if test x"$show_help" = x"yes" ; then
cat << EOF
Usage: configure [options]
Options: [defaults in brackets after descriptions]

EOF
echo "Standard options:"
echo "  --help                  print this message"
echo "  --decaf-path=PATH       path to DECAF (must be specified)"
echo "  --trace60               generate traces v60"
echo "  --trace50               generate traces v50 (default)"
echo "  --state40               generate state files v40"
echo "  --state20               generate state files v20 (default)"
echo "  --latest                generate traces v60 and state files v40"
echo ""
echo "Advanced options (experts only):"
echo "  --enable-taint       enable taint IR generation via TCG"
echo "  --disable-taint      disable taint IR generation via TCG (default)"
echo ""
exit 1
fi

if [[ ! $decaf_path ]] ; then
  echo
  echo "ERROR: Path to DECAF does not exist. Please double check"
  exit
fi

# Get DECAF version
decaf_version=`basename $decaf_path`
minor_idx=$((${#decaf_version}-1))
decaf_minor_version=${decaf_version:$minor_idx:1}

# Check we have at least DECAF1.7
if [ $decaf_minor_version -lt 7 ]; then
  echo
  echo "ERROR: TFD 1.1 requires at least DECAF1.7 but your version is $decaf_version"
  exit
fi

# Print info 
echo plugin path: $plugin_path
echo DECAF path: $decaf_path
echo trace version: $trace_version
echo state version: $state_version
echo taint enabled: $tcg_taint


config_h="config.h"
config_mak="config-plugin.mak"
test -f $config_h && mv $config_h ${config_h}~
echo "# Automatically generated by configure - do not modify" > $config_mak

echo "TARGET_DIR=$target" >> $config_mak
echo "SRC_PATH=$decaf_path" >> $config_mak
echo "PLUGIN_NAME=tfd" >> $config_mak
if test "$trace_version" = "50" ; then
  echo "TRACE_VERSION_50=y" >> $config_mak
fi
if test "$state_version" = "20" ; then
  echo "STATE_VERSION_20=y" >> $config_mak
fi
if test "$tcg_taint" = "yes" ; then
  echo "TAINT_ENABLED=y" >> $config_mak
fi

echo "#define PLUGIN_TFD" >> $config_h 
echo "#define DECAF_HOME \"$decaf_path\"" >> $config_h
echo "#define PLUGIN_PATH \"$plugin_path\"" >> $config_h
if test "$trace_version" = "50" ; then
  echo "#define TRACE_VERSION_50 1" >> $config_h
fi
if test "$state_version" = "20" ; then
  echo "#define STATE_VERSION_20 1" >> $config_h
fi
if test "$tcg_taint" = "yes" ; then
  echo "#define TAINT_ENABLED 1" >> $config_h
fi

